package com.jwt.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jwt.boot.entity.User;
import com.jwt.boot.service.UserService;
import com.jwt.boot.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @date 2020/10/4 14:02
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    UserService userService;


    @PostMapping("/login")
    public String login(@RequestParam("username") String username,@RequestParam("password") String password){
        User user = userService.getOne(new QueryWrapper<User>().eq("username", username));
        if (StringUtils.isEmpty(user)){
            log.error("用户不存在");
            return "用户不存在";
        }else {
            if (user.getPassword().equals(password)){
                log.info("登录成功:[{}]",username);
//                生成token令牌（根据用户id生成）
                String token = JWTUtil.generateToken(user.getId());
                return "登录成功"+token;
            }else {
                log.error("密码错误");
                return "密码错误";
            }
        }
    }

    @RequestMapping("/info")
    public List<User> info() {
        List<User> list = userService.list();
        return list;
    }
}
